package com.leilei;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author lei
 * @version 1.0
 * @date 2021/3/9 22:28
 * @desc flink 数据合并  Union 特性
 * union 可同时合并多个（只能相同类型）数据源，合并后可直接操作（计算或sink）
 */
public class UnionOperator {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setRuntimeMode(RuntimeExecutionMode.AUTOMATIC);
        env.setParallelism(1);
        DataStreamSource<String> source = env.fromElements("zs", "li", "we");
        DataStreamSource<String> source2 = env.fromElements("zs2", "li2", "we2");
        DataStreamSource<String> source3 = env.fromElements("zs3", "li3", "we3");
        DataStream<String> union = source.union(source2, source3);
        SingleOutputStreamOperator<String> streamOperator = union.map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                return value.toUpperCase();
            }
        });
        streamOperator.print("union").setParallelism(1);
        env.execute();
    }
}
